<template>
    <div class="login">
        <div class="login-box">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm" >
            <el-form-item  prop="username" >
                <el-input type="text" placeholder="账号" style="width:300px" v-model="ruleForm.username" autocomplete="off"> <i slot="prefix" class="el-input__icon el-icon-user"></i></el-input>
            </el-form-item>
            <el-form-item prop="password" >
                <el-input v-model="ruleForm.password" placeholder="密码" style="width:300px" type="password" show-password> <i slot="prefix" class="el-input__icon el-icon-lock"></i></el-input>
            </el-form-item>
            <div class="code">
                <el-form-item  class="code-input" prop="vercode">
                <el-input  v-model="ruleForm.vercode" placeholder="验证码" @keyup.enter.native=" submitForm('ruleForm')" style="width:170px;"> <i slot="prefix" class="el-input__icon el-icon-connection"></i></el-input>
            </el-form-item>
              <div class="code-img"><img v-show="vercodeImgSrc" :src="vercodeImgSrc" @click="refVercode()" /></div>
            </div>
            <el-form-item >
                <el-button style="margin-left:0px;width:300px" type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
            </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import { vercode } from '@/api/login'
import { mapActions } from 'vuex';
export default {
 name: 'Login',
  data() {
      var validateUserNum = (rule, value, callback) => {
         if (!value) {
          return callback(new Error('请输入账号'))
        }else if(value.length<4){
          return callback(new Error('账号不能小于4位'))
        }else if(value.length>12){
          return callback(new Error('账号不能大于12位'))
        }else {
            callback()
        }
      };
      var validatePass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请输入密码'))
        }else if(value.length<4){
          return callback(new Error('密码不能小于4位'))
        }else if(value.length>17){
              return callback(new Error('密码不能大于17位'))
        }else {
          callback()
        }
      };
      return {
        ruleForm: {
          username: 'superAdmin',
          password: 'jeepay666',
          vercode:'',
          vercodeToken:''
        },
         vercodeImgSrc: '', // 验证码图片 
          vercodeToken: '', // 验证码验证token
        rules: {
          username: [
            { validator: validateUserNum, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          vercode: [
            {  required: true, trigger: 'blur',message:'请输入验证码'}
          ]
        }
      };
    },
    mounted () {
        this.refVercode()
    },
    methods: {
    ...mapActions(['Login','Logout']),
     submitForm(formName) {
       const that = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const loginParams = {...this.ruleForm}
            //isSaveStorage为true时，将把token保存在localStorage中，否则保存在内存中。
            that.Login({loginParams,isSaveStorage:true})
            .then(res=>{
              this.loginSuccess(res)
            })
            .catch(err=>{
               console.log(err);
                this.$message({
                  showClose: true,
                  message: err.msg,
                  type: 'error',
                  duration:1000,//显示时间ms
                  offset: 50,//距离顶部偏移量
                });
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      loginSuccess(res){
          this.$router.push({path:'/'},()=>{})
      },
      
      refVercode () { // 刷新图片验证码
      const that = this
      // 获取图形验证码
      vercode().then(res => {
          that.vercodeImgSrc = res.imageBase64Data
          that.vercodeToken = res.vercodeToken
          this.ruleForm.vercodeToken =  res.vercodeToken
          this.ruleForm.vercode = ''
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  
}
</script>

<style scoped lang="scss">

	.login{
		background-size:cover;
		height:100%;
		background-image:url(~@/assets/bg.jpg);
    min-height: 600px;
    min-width: 500px;
      /* 设置弹性容器 */
      display: flex;
      /* 设置水平居中 */
      justify-content: center;
      /* 设置垂直居中 */
      align-items: center;
	}

  .login-box{
      width: auto;
       background: #e5e9f2;
      border-radius: 5px;
      padding:0 20px; 
      padding-top:20px;
  }
   .code {
    display: flex;
    justify-content: left;
    width:243px;
    .code-input {
      width: 170px;
    }
    .code-img {
      width: 117px;
      height: 40px;
       margin-left: 10px;
      background-color: #ddd;
        cursor: pointer;
      img{
        width: 117px;
        height: 40px;
      }
    }
  }
</style>